//
//  CalloutView.m
//  ENGAGEcx
//
//  Created by Phuong Le on 5/30/13.
//  Copyright (c) 2013 StarPound. All rights reserved.
//

#import "CalloutView.h"
#import "UIConstants.h"
#import <QuartzCore/QuartzCore.h>

@implementation CalloutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initialize];
    }
    
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    [self setBackgroundColor:[UIColor clearColor]];
    _arrowPos = ArrowPos_BottomRight;
    _alphaColor = CALLOUTVIEW_BACKGROUND_COLOR;//not use whiteColor causing clear
    _borderColor = CALLOUTVIEW_BORDER_COLOR;
    _padding = UIEdgeInsetsMake(8, 8, 8, 8);
    self.layer.masksToBounds = NO;
    self.layer.shadowRadius = 7;
    self.layer.shadowOpacity = 0.7;
}

- (void)drawRect:(CGRect)rect
{
    const CGFloat ROUND_RECT_CORNER_RADIUS = 2.0;
    rect.origin.x += _padding.left;
    rect.origin.y += _padding.top;
    rect.size.width = rect.size.width - _padding.left - _padding.right;
    rect.size.height = rect.size.height - _padding.top - _padding.bottom;
    
	CGPathRef roundRectPath;
    roundRectPath = [self drawBorder:rect cornerRadius:ROUND_RECT_CORNER_RADIUS];
    CGPathRef lineRectPath = CGPathCreateCopy(roundRectPath);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    const CGFloat *components = CGColorGetComponents(_alphaColor.CGColor);
    CGContextSetRGBFillColor(context, components[0], components[1], components[2], components[3]);
    CGContextAddPath(context, roundRectPath);
    
    //fill path
    CGContextFillPath(context);
	CGPathRelease(roundRectPath);
    
    //draw line
    UIColor *color = _borderColor;
    CGContextSetStrokeColorWithColor(context, color.CGColor);
    
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 1.0);
    CGContextAddPath(context, lineRectPath);
    
    // and now draw the Path!
    CGContextStrokePath(context);
}

- (CGPathRef)drawBorder:(CGRect)rect cornerRadius:(CGFloat)cornerRadius
{
	//
	// Create the boundary path
	//
	CGMutablePathRef path = CGPathCreateMutable();
    
	CGPathMoveToPoint(path, NULL,
                      rect.origin.x,
                      rect.origin.y + rect.size.height - cornerRadius);
    
	// Top left corner
	CGPathAddArcToPoint(path, NULL,
                        rect.origin.x,
                        rect.origin.y,
                        rect.origin.x + rect.size.width,
                        rect.origin.y,
                        cornerRadius);
    
    //
    if (_arrowPos == ArrowPos_TopCenter)
    {
        int x = rect.origin.x + rect.size.width/2 - 8;
        
        CGPathAddLineToPoint(path, NULL, x,
                             rect.origin.y);
        
        CGPathAddLineToPoint(path, NULL, x + 8,
                             rect.origin.y - 8);
        
        CGPathAddLineToPoint(path, NULL, x + 16,
                             rect.origin.y);
    }
    
	// Top right corner
	CGPathAddArcToPoint(path, NULL,
                        rect.origin.x + rect.size.width,
                        rect.origin.y,
                        rect.origin.x + rect.size.width,
                        rect.origin.y + rect.size.height,
                        cornerRadius);
    
	// Bottom right corner
	CGPathAddArcToPoint(path, NULL,
                        rect.origin.x + rect.size.width,
                        rect.origin.y + rect.size.height,
                        rect.origin.x,
                        rect.origin.y + rect.size.height,
                        cornerRadius);
    
    //
    if (_arrowPos == ArrowPos_BottomRight)
    {
        int x = rect.origin.x + rect.size.width - 10;
        
        CGPathAddLineToPoint(path, NULL, x,
                             rect.origin.y + rect.size.height);
        
        CGPathAddLineToPoint(path, NULL, x - 5,
                             rect.origin.y + rect.size.height + 8);
        
        CGPathAddLineToPoint(path, NULL, x - 10,
                             rect.origin.y + rect.size.height);
    }
    else if (_arrowPos == ArrowPos_BottomCenter)
    {
        int x = rect.origin.x + rect.size.width/2 + 8;
        
        CGPathAddLineToPoint(path, NULL, x,
                             rect.origin.y + rect.size.height);
        
        CGPathAddLineToPoint(path, NULL, x - 8,
                             rect.origin.y + rect.size.height + 8);
        
        CGPathAddLineToPoint(path, NULL, x - 16,
                             rect.origin.y + rect.size.height);
    }
    
	// Bottom left corner
	CGPathAddArcToPoint(path, NULL,
                        rect.origin.x,
                        rect.origin.y + rect.size.height,
                        rect.origin.x,
                        rect.origin.y,
                        cornerRadius);
    
	// Close the path at the rounded rect
	CGPathCloseSubpath(path);
	
	return path;
}

- (CGPathRef)drawArrow:(CGMutablePathRef)path
{
    return path;
}

@end
